package com.example.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.example.coupon.entity.CouponTemplate;
import com.example.coupon.exception.CouponException;
import com.example.coupon.service.IBuildTemplateService;
import com.example.coupon.service.ITemplateBaseService;
import com.example.coupon.vo.CouponTemplateSDK;
import com.example.coupon.vo.TemplateRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 优惠券模版相关的功能控制器
 *
 * @author wangbo
 * @description:
 * @date 2021/2/1815:06
 */
@Slf4j
@RestController
public class CouponTemplateController {

    /**
     * 构建优惠券模版服务
     */
    @Resource
    private IBuildTemplateService buildTemplateService;

    /**
     * 优惠券模版基础服务
     */
    @Resource
    private ITemplateBaseService templateBaseService;

    /**
     * 构建优惠券模版
     * @param request
     * @return
     * @throws CouponException
     */
    @PostMapping("/template/build")
    public CouponTemplate buildTemplate(@RequestBody TemplateRequest request) throws CouponException {
        log.info("Build Template:{}", JSON.toJSONString(request));
        return buildTemplateService.buildTemplate(request);
    }

    /**
     * 构造优惠券模版详情
     * @param id
     * @return
     * @throws CouponException
     */
    @GetMapping("/template/info")
    public CouponTemplate buildTemplateInfo(@RequestParam("id")Integer id) throws CouponException{
        log.info("Build Template Info For: {}",id);
        return templateBaseService.buildTemplateInfo(id);
    }

    /**
     * 查找所有可用的优惠券模版
     * @return
     */
    @GetMapping("/template/sdk/all")
    public List<CouponTemplateSDK> findAllUsableTemplate(){
        log.info("Find All Usable Template.");
        return templateBaseService.findAllUsableTemplate();
    }

    /**
     * 获取模版ids到CouponTemplateSDK的映射
     * @param ids
     * @return
     */
    @GetMapping("/template/sdk/infos")
    public Map<Integer,CouponTemplateSDK> findIds2TemplateSDK(@RequestParam("ids") Collection<Integer> ids){
        log.info("FindIds2TemplateSDK: {}",JSON.toJSONString(ids));
        return templateBaseService.findIds2Template(ids);
    }

}
